import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

/*
 *Description:
 *User:laoHu
 *Created with Intellij IDEA 2025
 *Data:2025.06.16
 *Time:16:46
 */

class TestClass {

    private static void testMethod(){

        System.out.println("testMethod");

    }

    public static void main(String[] args) {

        ((TestClass)null).testMethod();

    }

}

class Person implements Cloneable{
  String name;
  int age;

    @Override
    protected Object clone()throws CloneNotSupportedException{
        return super.clone();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Config{
    File file;
    //声明IOException异常
    public void openConfig(String fileName) throws IOException{
        if(!fileName.equals("texs.txt")){
            throw new FileNotFoundException("文件名不同");
        }
        if(!fileName.endsWith(".int")){
            throw new IOException("不是.int文件");
        }
    };
    public void main1(String[] args) throws IOException{
        Config config = new Config();
        config.openConfig("tex.txt");

        }
    }
    class TestException{
    private String name = "lisi";
    private String passWord = "8888888";

    public TestException(String name,String passWord) throws LoginNameException,LoginPasswordException{
        if(!this.name.equals(name)){
           // throw new LoginNameException("名字不匹配！");
        }
        if(!this.passWord.equals(passWord)){
            throw new LoginPasswordException("密码不匹配！");
        }
    }
}
public class Main extends Person{

    public static void main(String[] args) {

        try{
            TestException t = new TestException("lisi","666666");
        }catch(LoginNameException e){
            e.printStackTrace();
        }catch (LoginPasswordException e){
            e.printStackTrace();
        }finally{
            System.out.println("已处理异常！");
        }
    }

//    public void getCustomerInfo() throws java.io.FileNotFoundException{
//
//        try {
//
//            // do something that may cause an Exception
//
//        } catch (java.io.FileNotFoundException ex) {
//
//            System.out.print("FileNotFoundException!");
//
//        } catch (java.io.IOException ex) {
//
//            System.out.print("IOException!");
//
//        } catch (java.lang.Exception ex) {
//
//            System.out.print("Exception!");
//
//        }
//
//    }

    //finally
    public static int getData(){
        Scanner scanner = null;
        try{
            scanner = new Scanner(System.in);
            int data = scanner.nextInt();
            return data;
        }catch (InputMismatchException e){
            e.printStackTrace();
        }finally{
            System.out.println("finally执行了....");
            if(scanner != null) scanner.close();//回收资源
        }
        //捕捉到异常，输出异常原因，执行finaly，执行最后finally后的语句
        //没有捕获异常，直接返回输入值
        System.out.println("try-catch-finally后的程序");
        scanner.close();
        return -1;
    }
    public static void main8(String[] args) {
        int data = getData();
        System.out.println("data = " + data);
    }

    //处理多个异常是种类catch在前，父类异常在后
    public static void main7(String[] args) {
        try{
//            int a = 1;
//            int b = 0;
//            System.out.println(a / b);//算术异常
            //后面程序不执行
            byte[] bytes = {1,2,3};
            System.out.println(bytes[3]);
        }catch(ArithmeticException e){
            System.out.println("捕获到了算术异常！");
        }catch(NullPointerException e){
            System.out.println("捕获了空指针异常！");
        }
        catch(Exception e){
            System.out.println("捕获了异常！");
           e.printStackTrace();
        }
        System.out.println("所有异常已处理完成！");
    }

    //catch处理多个异常
    public static void main6(String[] args) {
        try{
            int[] arrays = new int[]{1,2,3};
            System.out.println(arrays[3]);//数组越界
            System.out.println(arrays);//不会执行
        }catch(NullPointerException e){
            e.printStackTrace();

        }catch (ArrayIndexOutOfBoundsException e){
            System.out.print("捕获到正真异常原因：");
            e.printStackTrace();

        }
        //System.out.println("捕获不到正真异常原因！！！");
    }

    //异常处理 try -catch
    public static void main5(String[] args) {
        Config config = new Config();
        try{
            config.openConfig("texs.txt");
        }catch(IOException e){
            System.out.println(e);//异常种类：异常信息
            System.out.println(e.getMessage());//异常信息
            e.printStackTrace();//详细信息，最后执行
        }
        System.out.println("异常已处理！");
    }

    //throws关键字，声明异常
    public static void main4(String[] args) throws CloneNotSupportedException {

        Person person = new Person();
        Person person1 = (Person) person.clone();
        System.out.println(person1);
    }

    public static int getElement(int[] arr,int index){

        if(arr == null){
            //throw new NullPointerException("空指针异常");
        }
        if(index < 0 && index >= arr.length){
            throw new ArrayIndexOutOfBoundsException("数组越界异常");
        }

        return arr[index];
    }
    //throw异常,主动检测，预防异常
    public static void main3(String[] args) {
        int[] arr = new int[3];
        int[] arr1 = null;
        getElement(arr,3);
     }

    //编译时异常：.CloneNotSupportedException
    //编译期间发生的异常，还未生成可执行程序
    public static void main2 (String[] args)throws CloneNotSupportedException {
        Person person = new Person();
        person.clone();
    }

    //常见异常:运行时异常
    public static void main1(String[] args){
        //算术异常
        int i = 10;
        int j = 0;
        //System.out.println(i/j);//运行时异常
        //java.lang.ArithmeticException: / by zero

        //数组越界异常
        int[] arrays = new int[10];
        for (int k = 0; k < arrays.length; k++) {
           // System.out.println(arrays[10]);
            //ArrayIndexOutOfBoundsException
        }

        //空指针引用异常
        String s = null;
        //System.out.println(s.charAt(0));
        //NullPointerException
    }
}
